Type TVector
	Field x:Float,y:Float,Z:Float
	
	Method Set(x#,y#,z#)
		Self.x = x
		Self.y = y
		Self.z = z
	End Method
End Type

Function RenderDebugCollision(body:Byte Ptr,vertexCount:Int,faceVertec:Float Ptr,id:Int)
	Local i:Int
	Local p0:TVector
	Local p1:TVector
	Local irr_p1:Vector3df
	Local irr_p2:Vector3df
	Local active:Int = NewtonBodyGetSleepState(body)
	Local color:SColor
	Select active
		Case 0
			color = SColor.createFromVals(255,0,255,0)
		Case 1
			color = SColor.createFromVals(255,255,0,0)
	End Select
	
	i = vertexcount - 1
	p0=  New TVector
	p0.x = faceVertec[i * 3 + 0]
	p0.y = faceVertec[i * 3 + 1]
	p0.z = faceVertec[i * 3 + 2]
	For i = 0 To vertexcount-1
		p1 = New TVector
		p1.x = faceVertec[i * 3 + 0] '* IRRTONEWT
		p1.y = faceVertec[i * 3 + 1] '* IRRTONEWT
		p1.z = faceVertec[i * 3 + 2] '* IRRTONEWT
		irr_p1 = Vector3df.createFromVals(p0.x,p0.y,p0.z)
		irr_p2 = Vector3df.createFromVals(p1.x,p1.y,p1.z)
		TIrrlicht.irr_video.Draw3DLine(irr_p1,irr_p2,color)
 		p0 = p1
	Next
End Function


'Helper Functions for Physic
Function GetMemory:Byte Ptr(size:Int)
		Return MemAlloc(size)
End Function

Function FreeMemory(p:Byte Ptr,size:Int)
		MemFree(p)
End Function

Rem
Function SaveObjectData(url:String , NO:TNewtonObject)
	Local Stream:Int = fopen_(url , "wb") 
     NewtonCollisionSerialize(NO.Body,Serialize , Byte Ptr Stream)
End Function
End Rem

Function Serialize(handle:Byte Ptr , Buffer:Byte Ptr , Length:Int) 
	   fwrite_(Buffer, length, 1, Int(Handle))
End Function

Function DeSerialize(handle:Byte Ptr , Buffer:Byte Ptr , Length:Int) 
	   fread_(Buffer, length, 1, Int(Handle))
End Function

Function NewtonBodyGetUserData_:Object(newtonbody:Byte Ptr)
	Local o:Object
	o=NewtonBodyGetUserData(newtonbody)
	If Int(objectpointer(o))=0 o=Null
	Return o
	
	Function ObjectPointer:Byte Ptr(o:Object)
		If o=Null Return Null
		Local p:Byte Ptr
		p=o
		p:-8
		Return p
	EndFunction
EndFunction


Global PhysicTimer:Deltatime = DeltaTime.Create(60)

Function getFPS:Int()
	Return DeltaTime.FPS(PhysicTimer)
End Function

Function GetDelta:Double()
	Return Deltatime.currDelta(PhysicTimer)
End Function

Function DeltaUpDate()
	DeltaTime.Update()
End Function


Type DeltaTime

	Global DeltaTList:TList

	Field OldTime:Double
	Field ElapsedTime:Double
	Field Delta:Double
	Field dFps:Double
	Field dDelta:Double
	Field CurrFps:Double
	Field FPSCounter:Double
	Field CurrTime:Double
	Field CheckTime:Double
	
		Method New ()
            	If DeltaTList = Null Then DeltaTList = New TList
            	DeltaTList.AddLast Self
    	End Method		
		
		Method Destroy ()
            	DeltaTList.Remove Self
        End Method

	
		Function Create:DeltaTime(Fps:Double)
			Local dTime:DeltaTime = New DeltaTime
			dTime.Delta = 1
			dTime.dFps = Fps
			dTime.dDelta = 1000/Fps
			Return dTime
		End Function
		
		Function Update()
			Local dTime:DeltaTime
			For dTime:DeltaTime = EachIn DeltaTList
				dTime.CurrTime = MilliSecs()
				dTime.ElapsedTime = dTime.CurrTime - dTime.OldTime
				dTime.OldTime = dTime.CurrTime
				
				If dTime.CurrTime > dTime.CheckTime
					dTime.CheckTime = dTime.CurrTime + 1000
					dTime.CurrFps = dTime.FPSCounter
					dTime.FPSCounter = 0
				Else
					dTime.FPSCounter:+1
				EndIf
			Next
			
		End Function
		
		Function FPS:Double(dTime:DeltaTime)
			Return dTime.CurrFps
		End Function
		
		Function CurrDelta:Double(dTime:DeltaTime)
			Return dTime.ElapsedTime/dTime.dDelta
		End Function
		
End Type

